package com.horay.springclouddemo.provider.member.controller;

import com.horay.springclouddemo.common.http.Responses;
import com.horay.springclouddemo.common.http.ResultBody;
import com.horay.springclouddemo.model.member.Member;
import com.horay.springclouddemo.model.member.MemberDTO;
import com.horay.springclouddemo.provider.member.service.MemberService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 成员控制器
 *
 * @author heyingcheng
 * @date 2020/3/21 13:52
 */
@RestController
@RequestMapping("member")
public class MemberController {

    private final MemberService memberService;

    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @PostMapping
    public ResponseEntity<ResultBody<String>> createMember(Member member) {
        if (memberService.createMember(member)) {
            return Responses.custom(HttpStatus.OK.value(), "createMember", HttpStatus.OK.name());
        } else {
            return Responses.unprocessableEntity(HttpStatus.UNPROCESSABLE_ENTITY.name());
        }
    }

    @GetMapping("{id}")
    public ResponseEntity<ResultBody<Member>> getMember(@PathVariable Integer id) {
        return Responses.custom(HttpStatus.OK.value(), "getMember", memberService.getMember(id));
    }

    @GetMapping("members")
    public ResponseEntity<ResultBody<List<Member>>> getMembers(@RequestBody MemberDTO memberDTO) {
        return Responses.custom(HttpStatus.OK.value(), "getMembers", memberService.getMembers(memberDTO));
    }

    @PutMapping
    public ResponseEntity<ResultBody<String>> updateMember(Member member) {
        if (memberService.updateMember(member)) {
            return Responses.custom(HttpStatus.OK.value(), "updateMember", HttpStatus.OK.name());
        } else {
            return Responses.unprocessableEntity(HttpStatus.UNPROCESSABLE_ENTITY.name());
        }
    }

    @DeleteMapping("{id}")
    public ResponseEntity<ResultBody<String>> deleteMember(@PathVariable Integer id) {
        if (memberService.deleteMember(id)) {
            return Responses.custom(HttpStatus.OK.value(), "deleteMember", HttpStatus.OK.name());
        } else {
            return Responses.unprocessableEntity(HttpStatus.UNPROCESSABLE_ENTITY.name());
        }
    }

}
